﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using weikebuluo.DAL;
using weikebuluo.Common;
using weikebuluo.Model;

namespace weikeweixin.web.wechat.vip
{
    public partial class index : System.Web.UI.Page
    {
        v_vip_dal vipdal = new v_vip_dal();
        protected v_vip_model vipmodel = new v_vip_model();
        protected DataTable dtvip = new DataTable();
        v_cardset_dal cardsetdal = new v_cardset_dal();
        protected v_cardset_model cardsetmodel= new v_cardset_model();

        public string cardset_bgpic = "../../admin/all/images/0618587298.png";


        v_explain_dal explaindal = new v_explain_dal();
        protected v_explain_model explainmodel = new v_explain_model();

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = explaindal.GetList("user_id=" + Request.QueryString["uid"]).Tables[0];
            if (dt.Rows.Count > 0)
            {
                explainmodel = explaindal.GetModel(int.Parse(dt.Rows[0]["explain_id"].ToString()));
            }

            if (!string.IsNullOrEmpty(Request.QueryString["uid"])) {//获取背景图片
                DataTable dtcardset = cardsetdal.GetList("user_id=" + Request.QueryString["uid"]).Tables[0];
                cardsetmodel = cardsetdal.GetModel(int.Parse(dtcardset.Rows[0]["cardset_id"].ToString()));
                if (!string.IsNullOrEmpty(cardsetmodel.cardset_bgpic.ToString()))
                {
                    cardset_bgpic = cardsetmodel.cardset_bgpic;
                }
            }

            //判断是否领卡
            if (!string.IsNullOrEmpty(Request.QueryString["wxid"]))
            {
                dtvip = vipdal.GetList("openid='" + Request.QueryString["wxid"] + "'").Tables[0];
                if (dtvip.Rows.Count > 0)
                {
                    vipmodel = vipdal.GetModel(int.Parse(dtvip.Rows[0]["vip_id"].ToString()));
                    if (!string.IsNullOrEmpty(Request.QueryString["hid"])) {
                        Response.Write("<script Language=\"Javascript\">location.href=\"/web/wechat/guaguaka/guaguaka.aspx?wxid=" + Request.QueryString["wxid"] + "&uid=" + Request.QueryString["uid"] + "&hid=" + Request.QueryString["hid"] +"\";");
                        Response.Write("</script>");
                        Response.End();
                    }
                }


            }
            
            switch (Request.QueryString["action"]) {
                case "bindvip":
                    submit();
                break;
            }
        }


        private void submit() { //领卡
            DataTable dt = vipdal.GetList("vip_phone='" + Request.Form["phone"] + "and user_id=" + Request.QueryString["uid"] + "'").Tables[0];
            if (dt.Rows.Count > 0) {
                Response.Write("{\"error\":\"电话号码已经注册！\"}");
                Response.End();
            }

            vipmodel.vip_name = Request.Form["name"].Trim().ToString();
            vipmodel.vip_phone = Request.Form["phone"].Trim().ToString();
            vipmodel.user_id = int.Parse(Request.QueryString["uid"].ToString());
            if (Request.QueryString["wxid"].Trim().ToString() == "null")
            {
                Response.Write("{\"error\":\"请从微信登录！\"}");
                Response.End();
            }
            vipmodel.openid = Request.QueryString["wxid"].Trim().ToString();
            vipmodel.vip_code = 100+getcode();
            if (vipdal.Add(vipmodel) > 0)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["hid"])) {
                    Response.Write("{\"error\":\"100\",\"msg\":\"\",\"url\":\"/web/wechat/guaguaka/guaguaka.aspx?wxid=" + Request.QueryString["wxid"] + "&uid=" + Request.QueryString["uid"] + "&hid=" + Request.QueryString["hid"] + "\"}");
                    Response.End();
                }
                else
                {
                    Response.Write("{\"error\":\"200\",\"msg\":\"\",\"url\":\"index.aspx?wxid=" + Request.QueryString["wxid"] + "&uid=" + Request.QueryString["uid"] + "\"}");
                    Response.End();
                }
            }
            else {
                Response.Write("{\"error\":\"获得会员卡失败！\"}");
                Response.End();
            }

        }

        #region 生成卡号
        /// <summary>
        /// 生成卡号
        /// </summary>
        /// <returns></returns>
        private string getcode()
        {
            Random ran = new Random();

            string vipcode = "";

            for (int j = 0; j < 5; j++)
            {
                vipcode += ran.Next(0, 9);
            }
            for (int i = 0; 1 == 1; i++)
            {
                int reInt = vipdal.GetRecordCount("vip_code='" + vipcode + "'");

                if (reInt < 1)
                {
                    break;
                }
                else
                {
                    vipcode = "";

                    for (int j = 0; j < 5; j++)
                    {
                        vipcode += ran.Next(0, 9);
                    }
                }
            }
            return vipcode;
        }
        #endregion

    }
}